﻿/* ==============================================================================
 * 功能描述：CharacterPanelGoodsTips  
 * 
 * 创 建 者：郑镇泉
 * 
 * 创建日期：2016/4/10 12:02:27
 * ==============================================================================*/
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using ZZQFrameWork;

public class CharacterPanelGoodsTips : MonoBehaviour
{
    /// <summary>
    /// 物品名称
    /// </summary>
    private Text goodsNameText;

    /// <summary>
    /// 属性信息
    /// </summary>
    private Text attrInfoText;

    /// <summary>
    /// 物品图标
    /// </summary>
    private Image goodsIconImage;

    /// <summary>
    /// 属性排列grid
    /// </summary>
    private GridLayoutGroup magicAttrGridLayoutGroup;

    /// <summary>
    /// 物品描述
    /// </summary>
    private Text desText;

    /// <summary>
    /// 等级需求
    /// </summary>
    private Text lvNeedText;

    /// <summary>
    /// 当前存在的attrItem集合
    /// </summary>
    private List<MagicAttrItem> attrItemList = new List<MagicAttrItem>(); 

    /// <summary>
    /// 当前存在的magicAttrItem集合
    /// </summary>
    //private List<MagicAttrItem> magicAttrItemList=new List<MagicAttrItem>(); 

    /// <summary>
    /// 魔法属性预设
    /// </summary>
    private GameObject magicAttrItemPrefab;

    public GameObject MagicAttrItemPrefab
    {
        get {
            if (magicAttrItemPrefab == null)
            {
                magicAttrItemPrefab=ResManager.Instance.Load("MagicAttrItem")as GameObject;
            }
            return magicAttrItemPrefab;
        }
    }

    /// <summary>
    /// 装备资源图集
    /// </summary>
    private UIAtlas equipAtlas;

    public UIAtlas EquipAtlas
    {
        get
        {
            if (equipAtlas == null)
            {
                equipAtlas = UIAtlasManager.Instance.GetUIAtlasByName("Equip_Atlas");
            }
            return equipAtlas;
        }
    }

    /// <summary>
    /// 物品资源图集
    /// </summary>
    private UIAtlas goodsAtlas;

    public UIAtlas GoodsAtlas
    {
        get
        {
            if (goodsAtlas == null)
            {
                goodsAtlas = UIAtlasManager.Instance.GetUIAtlasByName("Goods_Atlas");
            }
            return goodsAtlas;
        }
    }

    void Awake()
    {
        goodsNameText = transform.GetComponentByPath<Text>("BG/GoodsName");
        attrInfoText = transform.GetComponentByPath<Text>("BG/AttrInfo");
        goodsIconImage = transform.GetComponentByPath<Image>("BG/GoodsItem/GoodsIcon");
        magicAttrGridLayoutGroup = transform.GetComponentByPath<GridLayoutGroup>("BG/MagicAttrGrid");
        desText = transform.GetComponentByPath<Text>("BG/Des");
        lvNeedText = transform.GetComponentByPath<Text>("BG/LvNeed");
    }

    void Start()
    {
        OnGoodsTipsShow(null);
    }

    void OnEnable()
    {
        EventDispatcher.AddListener<GoodsVO>(Events.CharacterEvent.OnGoodsTipsShow,OnGoodsTipsShow);
    }

    void OnDisable()
    {
        EventDispatcher.RemoveListener<GoodsVO>(Events.CharacterEvent.OnGoodsTipsShow, OnGoodsTipsShow);
    }

    /// <summary>
    /// 物品tips显示
    /// </summary>
    /// <param name="goods"></param>
    void OnGoodsTipsShow(GoodsVO goods)
    {
        if (goods == null)
        {
            goodsNameText.text = null;
            goodsIconImage.sprite = null;
            goodsIconImage.gameObject.SetActive(false);
            attrInfoText.text = null;
            for (int i = 0; i < attrItemList.Count; i++)
            {
                attrItemList[i].Data = null;
            }
            desText.text = null;
            lvNeedText.text = null;
            return;
        }
        goodsNameText.text = goods.name;
        attrInfoText.text = "攻击力：" + goods.attack + "\n" + goods.AtkSpeedName;
        goodsIconImage.sprite = goods.goodsType == Enum_GoodsType.Equip
            ? EquipAtlas.GetSprite(goods.icon)
            : GoodsAtlas.GetSprite(goods.icon);
        goodsIconImage.gameObject.SetActive(true);

        if (goods.attrDataList != null && goods.attrDataList.Count > 0)
        {
            List<AttrData> attrDataList = goods.attrDataList;
            attrItemList=PoolManager.Instance.SpawnNeedList<MagicAttrItem>("MagicAttrItem", attrDataList.Count, attrItemList);

            for (int i = 0; i < attrItemList.Count; i++)
            {
                attrItemList[i].gameObject.transform.SetParent(magicAttrGridLayoutGroup.transform,false);
                attrItemList[i].Data = attrDataList[i];
            }
        }
        else
        {
            for (int i = 0; i < attrItemList.Count; i++)
            {
                attrItemList[i].Data = null;
            }
        }

        desText.text = goods.des;
        if (goods.requirementDataList!=null&&goods.requirementDataList.Count>0)
        {
        RequirementData data = goods.requirementDataList[0];
        lvNeedText.text = "需要等级：" + data.lv + "\n" + "需要属性：" + data.attr.attrName + ":" + data.attr.attrValue;
        if (ModuleManager.Instance.Get<CharacterModule>().mainRole.lv < data.lv || ModuleManager.Instance.Get<CharacterModule>().mainRole.GetAttrDataByAttrType(data.attr.attrType).attrValue < data.attr.attrValue)
        {
            lvNeedText.color = Color.red;
        }
        else
        {
            lvNeedText.color = Color.green;
        }
        }
        else
        {
            lvNeedText.text = "";
        }
    }
}